import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;



public class ConnectGUI implements ActionListener {
	JPanel connectPanel;
	JButton buttonConnect;
	private static JFrame frmPapersoccer;
	private JTextField textPlayer1;
	private JTextField textServer;
	private JLabel serverStatus;

	private Container createContentPane() {
		// Create a JPanel to place everything on, a bit unnecessary with two for this content
		JPanel GUI = new JPanel();
		GUI.setLayout(null);

		// Creation of a Panel to contain the JLabels and other stuff
		connectPanel = new JPanel();
		connectPanel.setLayout(null);
		connectPanel.setLocation(10, 5);
		connectPanel.setSize(158, 189);
		GUI.add(connectPanel);

		buttonConnect = new JButton("Connect to server");
		buttonConnect.setBounds(10, 155, 136, 23);
		buttonConnect.addActionListener(this);
		connectPanel.add(buttonConnect);

		JLabel lblPlayer = new JLabel("Player");
		lblPlayer.setBounds(10, 11, 46, 14);
		connectPanel.add(lblPlayer);

		textPlayer1 = new JTextField();
		textPlayer1.setBounds(10, 36, 86, 20);
		connectPanel.add(textPlayer1);
		textPlayer1.setColumns(10);

		serverStatus = new JLabel("");
		serverStatus.setBounds(10, 125, 136, 14);
		connectPanel.add(serverStatus);

		textServer = new JTextField();
		textServer.setText("127.0.0.1");
		textServer.setBounds(10, 92, 86, 20);
		connectPanel.add(textServer);
		textServer.setColumns(10);

		JLabel lblServer = new JLabel("Server");
		lblServer.setBounds(10, 70, 46, 14);
		connectPanel.add(lblServer);
		return GUI;
	}

	public static void main(String[] arg)
	{
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Connect_GUI();
			}
			private void Connect_GUI() {
				ConnectGUI connect = new ConnectGUI();
				frmPapersoccer = new JFrame("Connect to Paper-soccer Server");
				frmPapersoccer.setTitle("Paper-soccer");
				frmPapersoccer.setResizable(false);

				frmPapersoccer.setContentPane(connect.createContentPane());
				frmPapersoccer.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				frmPapersoccer.setSize(182, 233);
				frmPapersoccer.setVisible(true); 
			}});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == buttonConnect)
		{
			final int PORT = 2000;
			InetAddress addr;
			Socket socket = null;
			serverStatus.setText("Waiting for another player!");		// Should display a text to the user while waiting

			// Debug to console
			System.out.println(textPlayer1.getText());
			System.out.println(textServer.getText());


			try {
				addr = InetAddress.getByName(textServer.getText()); 		     // Read the Address from text field
				socket = new Socket(addr, PORT);
				String result = textPlayer1.getText().replaceAll("[-+.^:,]",""); // Remove special characters
				System.out.println("The new socket: " + socket);			     // Debug text to console
				GameClient.GameGUI(true, result, socket);	                     // Send the socket and the Name back to GameGUI
				frmPapersoccer.dispose();									     // Dispose the window after connect
			}
			
			/* Some understandable error message */
			catch (ConnectException e1)
			{
				e1.printStackTrace();
				serverStatus.setForeground(Color.RED);
				serverStatus.setText("Connection refused!, Server running?");
			}

			catch (UnknownHostException net){
				System.out.println("Can not find the server.");
				System.exit(1);
			}

			catch (IOException e1) {
				e1.printStackTrace();
				serverStatus.setForeground(Color.RED);
				serverStatus.setText("Can not connect to server!");
			}
		}

	}
}
